package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.module.common.dao.ICityDao;
import com.rpframework.module.common.domain.City;
import com.rpframework.module.common.service.CityService;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.CProperty;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.CPropertyService;

/**
 * 城市列表 小区列表
 * @author Andy_hqr
 * @date 2016年3月8日 下午7:27:28
 */
@Controller
@RequestMapping("api/property")
public class ApiPropertyAct extends BaseAct{
	 Gson gson=new Gson();
	 @Resource CPropertyService cpropertyService;
	 @Resource CityService cityService;
	 
	 /**
	  * 1.获取小区不为空的城市列表 cityByVillage
	  * 2.根据js城市名称返回cityCode
	  * 3.根据cityCode 查询小区列表且不为空
	  * 4.实名认正面返回地址修改小区列表
	  * 5.用户注册 控制获取短信验证码的样式控制
	  * 6.当前登录用户所属小区的公告列表 做一个分页 api/widen/list
	  * 
	  * 
	  * 
	  */
	 
	 
	 @Resource ICityDao cityDao;
	 //城市名字
	 @RequestMapping(value="/cityone",produces = "application/json; charset=utf-8")
		public @ResponseBody String cityone(
				@RequestParam String code,
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			City worknost = cityDao.select(code);
				JsonObject json = new JsonObject();
				json.addProperty("code", worknost.getCode());
				json.addProperty("name", worknost.getName());
				if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
				return ""+json ;
		}
	//城市遍历
	 @RequestMapping(value="/citylist",produces = "application/json; charset=utf-8")
		public @ResponseBody String citylist(
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			List<City> worknost = cityService.queryAll();
			JsonObject json =new JsonObject();
			JsonArray array=new JsonArray();
			json.add("arrays", array);
			for (City act : worknost) {
				JsonObject jsonObj = new JsonObject();
				jsonObj.addProperty("code", act.getCode());
				jsonObj.addProperty("name", act.getName());
				array.add(jsonObj);
			}
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
	 	//根据城市遍历小区
	 	/**
	 	 * 根据cityCode查询小区列表
	 	 * @param cityCode
	 	 * @param callback
	 	 * @param session
	 	 * @return
	 	 * @time 2016年3月8日 下午7:38:20
	 	 */
		@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
		public @ResponseBody String listone(@RequestParam String cityCode,
				@RequestParam(value = "callback",required = false ) String callback,
				
				HttpSession session){
			List<CProperty> worknost = cpropertyService.listcity(cityCode);
			JsonObject json =new JsonObject();
			JsonArray array=new JsonArray();
			json.add("arrays", array);
			for (CProperty act : worknost) {
				JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
				array.add(jsonObj);
			}
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		
		//物业公司介绍
		@RequestMapping(value="/seleProperty",produces = "application/json; charset=utf-8")
		public @ResponseBody String seleProperty(@RequestParam(value = "callback",required = false ) String callback,HttpSession session){
			
			User sessionuser=getSessionUser(session);
			if(sessionuser==null){
				throw new APICodeException(-4, "你还没登陆!");
			}
			CProperty cpropert= cpropertyService.selectcProperty(sessionuser.getEstateId()+"");
			JsonObject json =new JsonObject();
			json.addProperty("pic", cpropert.getPic());
			json.addProperty("communityname", cpropert.getCommunityname());
			json.addProperty("responsible", cpropert.getResponsible());
			json.addProperty("phone", cpropert.getPhone());
			json.addProperty("address", cpropert.getAddress());
			json.addProperty("msg", cpropert.getMsg());
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		
		//显示  小区
		@RequestMapping(value="/listBytoproperty",produces = "application/json; charset=utf-8")
		public @ResponseBody String listBytoproperty(@RequestParam(value="pager",required = false ) Pager<CProperty> pager,
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			if(pager==null){
				pager=new Pager<CProperty>();
			}
			
			cpropertyService.getpager(pager);
			JsonObject json=new JsonObject();
			json.addProperty("totalPages", pager.getTotalPages());
			json.addProperty("currentPage", pager.getCurrentPage());
			json.addProperty("totalCount", pager.getTotalCount());
			List<CProperty> list=pager.getItemList();
			JsonArray array=new JsonArray();
			json.add("arrays", array);
			for (CProperty act : list) {
				JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
				array.add(jsonObj);
			}
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		@RequestMapping(value="/listByid",produces = "application/json; charset=utf-8")
		public @ResponseBody String listByid(
				@RequestParam String id,
				@RequestParam(value = "callback",required = false ) String callback,
				HttpSession session){
			CProperty  cp=cpropertyService.selectcProperty(id);
			JsonObject json=new JsonObject();
			json.addProperty("name", cp.getCommunityname());
			json.addProperty("id", cp.getId());
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}

}
